package com.chenwy.example.learning.serialize;


import java.io.*;

public class SerializeTest {
    /**
     * 序列化
     * @param outputStream
     * @param o
     * @param <T>
     */
    public static <T> void serialize(T o,OutputStream outputStream) throws IOException {
       try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)){
           objectOutputStream.writeObject(o);
       }
    }

    /**
     * 反序列化
     * @param inputStream
     * @param <T>
     * @return
     */
    public static <T> T deserialize(InputStream inputStream) throws IOException, ClassNotFoundException {
        if(inputStream == null) return null;
        try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);) {
            Object o = objectInputStream.readObject();
            if(o != null) return (T) o;
            return null;
        }
    }

    public static <A,B> boolean isInstance( Class<A> a, B b){
        if(a == null || b == null) return false;
        return a.isInstance(b);
    }

    public static <P,C> boolean isAssignableFrom(P p, C c){
        if( p == null || c == null) return false;
        return p.getClass().isAssignableFrom(c.getClass());
    }

    public static void main(String[] args) {

    }
}
